const $time = {
  formatDuring(mss) {
    if (typeof mss === "string") {
      mss = parseInt(mss);
    }
    const days = parseInt(mss / (1000 * 60 * 60 * 24));
    const hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    const minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
    const seconds = (mss % (1000 * 60)) / 1000;
    return days + " 天 " + parseInt(hours) + " 小时 " + parseInt(minutes) + " 分钟 " + parseInt(seconds) + " 秒 ";
  },
  /**
   * 日期格式化
   * @param time 时间
   * @param type 格式化类型 1.年-月-日，2.时:分:秒，3.年-月-日 时:分，默认：年-月-日 时:分:秒
   * @param pattern 格式时间格式
   * @return string
   */
  parseTime(time, type, pattern) {
    if (arguments.length === 0 || !time) {
      return null;
    }
    let format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
    type = type || 0;
    if (!pattern) {
      switch (type) {
        case 1:
          format = "{y}-{m}-{d}";
          break;
        case 2:
          format = "{h}:{i}:{s}";
          break;
        case 3:
          format = "{y}-{m}-{d} {h}:{i}";
          break;
      }
    }
    let date;
    if (typeof time === "object") {
      date = time;
    } else {
      if (typeof time === "string") {
        time = new Date(time.replace(/-/g, "/").replace(/[TZ]/g, " ").trim());
      } else if (typeof time === "number" && time.toString().length === 10) {
        time = time * 1000;
      }
      date = new Date(time);
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay(),
    };
    return format.replace(/{([ymdhisa])+}/g, function (result, key) {
      let value = formatObj[key];
      // Note: getDay() returns 0 on Sunday
      if (key === "a") {
        return ["日", "一", "二", "三", "四", "五", "六"][value];
      }
      if (result.length > 0 && value < 10) {
        value = "0" + value;
      }
      return value || 0;
    });
  },
};
